/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.stuwork.service.impl;

import me.zhengjie.modules.stuwork.domain.Stuwork;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.stuwork.repository.StuworkRepository;
import me.zhengjie.modules.stuwork.service.StuworkService;
import me.zhengjie.modules.stuwork.service.dto.StuworkDto;
import me.zhengjie.modules.stuwork.service.dto.StuworkQueryCriteria;
import me.zhengjie.modules.stuwork.service.mapstruct.StuworkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wzy
* @date 2024-12-12
**/
@Service
@RequiredArgsConstructor
public class StuworkServiceImpl implements StuworkService {

    private final StuworkRepository stuworkRepository;
    private final StuworkMapper stuworkMapper;

    @Override
    public PageResult<StuworkDto> queryAll(StuworkQueryCriteria criteria, Pageable pageable){
        Page<Stuwork> page = stuworkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(stuworkMapper::toDto));
    }

    @Override
    public List<StuworkDto> queryAll(StuworkQueryCriteria criteria){
        return stuworkMapper.toDto(stuworkRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StuworkDto findById(Integer uid) {
        Stuwork stuwork = stuworkRepository.findById(uid).orElseGet(Stuwork::new);
        ValidationUtil.isNull(stuwork.getUid(),"Stuwork","uid",uid);
        return stuworkMapper.toDto(stuwork);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Stuwork resources) {
        stuworkRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Stuwork resources) {
        Stuwork stuwork = stuworkRepository.findById(resources.getUid()).orElseGet(Stuwork::new);
        ValidationUtil.isNull( stuwork.getUid(),"Stuwork","id",resources.getUid());
        stuwork.copy(resources);
        stuworkRepository.save(stuwork);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer uid : ids) {
            stuworkRepository.deleteById(uid);
        }
    }

    @Override
    public void download(List<StuworkDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StuworkDto stuwork : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程号", stuwork.getClassid());
            map.put("班级号", stuwork.getRoomid());
            map.put("姓名", stuwork.getName());
            map.put("第几周", stuwork.getWeek());
            map.put("日期", stuwork.getData());
            map.put("是否交作业", stuwork.getSubmit());
            map.put("是否题目全", stuwork.getIsall());
            map.put("学号+姓名是否齐全", stuwork.getIsname());
            map.put("是否源代码和输出齐全（未遮挡）", stuwork.getIsright());
            map.put("是否一一对应原题顺序", stuwork.getIsmatch());
            map.put("是否延时", stuwork.getIslate());
            map.put("具体情况（补充说明）", stuwork.getImaformation());
            map.put("得分", stuwork.getScore());
            map.put("学号", stuwork.getStuid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}